package com.mozai.shopping.service.impl;

import com.mozai.shopping.dao.ProductTypeDao;
import com.mozai.shopping.model.ProductType;
import com.mozai.shopping.model.vo.PageResp;
import com.mozai.shopping.service.ProductTypeService;
import com.mozai.shopping.utils.bean.ComonQueryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductTypeServiceImpl implements ProductTypeService {

    @Autowired
    ProductTypeDao productTypeDao;
    @Override
    public int insertProductService(ProductType productType) {
       return productTypeDao.insert(productType);
    }

    @Override
    public int delProductService(ProductType productType) {
        if (productTypeDao.selectByPrimaryKey(productType.getId()).getDelFlag()==0){
            productType.setDelFlag(1);
            return productTypeDao.updateByPrimaryKeySelective(productType);
        }
        return 0;
    }

    @Override
    public int updateProductService(ProductType productType) {
        if (productTypeDao.selectByPrimaryKey(productType.getId()).getDelFlag()==0){
            return productTypeDao.updateByPrimaryKeySelective(productType);
        }
        return 0;
    }

    @Override
    public PageResp<ProductType> queryProductType(ProductType productType, ComonQueryBean comonQueryBean) {

        //start
        if (comonQueryBean !=null && comonQueryBean.getPageNum() !=null && comonQueryBean.getPageSize() !=null){
            comonQueryBean.setStart((comonQueryBean.getPageNum()-1)*comonQueryBean.getPageSize());
        }
        //List
         List<ProductType> productTypeResult = productTypeDao.list4Page(productType,comonQueryBean);
        //Total
        int count = productTypeDao.count(productType);
        comonQueryBean.setTotal(count);
        //totalPage
        comonQueryBean.setTotalPage(ComonQueryBean.calculateTotalPage(comonQueryBean));

        PageResp<ProductType> productTypePageResp =new PageResp<>();
        productTypePageResp.setPageSize(comonQueryBean.getPageSize());
        productTypePageResp.setPageNum(comonQueryBean.getPageNum());
        productTypePageResp.setList(productTypeResult);
        productTypePageResp.setStart(comonQueryBean.getStart());
        productTypePageResp.setTotalPage(comonQueryBean.getTotalPage());
        productTypePageResp.setTotal(comonQueryBean.getTotal());
        return productTypePageResp;
    }
}
